/**
 * @Author Guy Franco
 *
 * 	this is the Approve Assess Duration Panel  able to search a duration and a request, present its details and 
 * update the DB if approved
 */

package gui;

import javax.swing.JPanel;

import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ApproveAssessDurationPanel extends JPanel implements ChatIF{
	private JTextField durationField;
	private JTextField setdurationField;
	private AcademicFrame frame;
	private ApproveAssessDurationPanel approveAssessDurationPanel;
	private SupervisorPanel supervisorPanel;
	private JComboBox rcomboBox;
	private ViewRequestPanel viewRequest;
	public ApproveAssessDurationPanel(AcademicFrame frame, SupervisorPanel supervisorPanel) {
		super();
		this.frame=frame;
		this.supervisorPanel=supervisorPanel;
		this.approveAssessDurationPanel=this;
		frame.setSize(500, 400);
		
		durationField = new JTextField();
		durationField.setEnabled(false);
		durationField.setBounds(192, 140, 148, 20);
		add(durationField);
		durationField.setColumns(10);
		
		setdurationField = new JTextField();
		setdurationField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent evt) {
				char k=evt.getKeyChar();
				if (!(k>='0' && k<='9'))
				        evt.consume();
			}
		});
		setdurationField.setBounds(124, 191, 175, 20);
		add(setdurationField);
		setdurationField.setColumns(10);
		
		
		
		
		initialize();
	}
		private void initialize()
		{
		
		setLayout(null);
		
		JLabel lblApproveAssessDuration = new JLabel("Approve Assess Duration");
		lblApproveAssessDuration.setForeground(Color.BLUE);
		lblApproveAssessDuration.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblApproveAssessDuration.setBounds(86, 22, 279, 20);
		add(lblApproveAssessDuration);
		
		JLabel lblSelectARequest = new JLabel("Select a Request");
		lblSelectARequest.setBounds(59, 70, 121, 14);
		add(lblSelectARequest);
		
		rcomboBox = new JComboBox();
		rcomboBox.setBounds(33, 95, 159, 20);
		add(rcomboBox);
		
		JButton btnDetails = new JButton("Details");
		btnDetails.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rcomboBox.getSelectedItem().equals("--Choose a Request--"))// filter invalid choice
				{
					durationField.setText("");
					(new Dialog("Please choose a request from the list first.")).setVisible(true);
				}
				else{
				viewRequest = new ViewRequestPanel(rcomboBox.getSelectedItem().toString(),frame,approveAssessDurationPanel,false,"");
				frame.setSize(550, 780);
				frame.setContentPane(viewRequest);
				}
	
			}
		});
		btnDetails.setBounds(269, 94, 89, 23);
		add(btnDetails);
		
		JButton btnDuration = new JButton("Duration");
		btnDuration.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(rcomboBox.getSelectedItem().equals("--Choose a Request--"))// filter invalid choice
				{
					(new Dialog("Please Select a Request ")).setVisible(true);
				}
				else
				{
				ArrayList<Object> duration =new ArrayList<Object>();
				duration.add("duration");
				duration.add(rcomboBox.getSelectedItem().toString());
				AcademicFrame.myclient.sendMessageToServer(duration);
				}
			}
		});
		btnDuration.setBounds(59, 139, 89, 23);
		add(btnDuration);
		
		
		
		JLabel lblDays = new JLabel("Days");
		lblDays.setBounds(364, 143, 46, 14);
		add(lblDays);
		
		JLabel lblSetNewDuration = new JLabel("Set New Duration:");
		lblSetNewDuration.setBounds(10, 194, 116, 14);
		add(lblSetNewDuration);
		
		
		
		JButton btnSetDuration = new JButton("Set Duration");
		btnSetDuration.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (setdurationField.getText().isEmpty()) {
					(new Dialog("No Duration Entered")).setVisible(true);

				}
				else
					if(rcomboBox.getSelectedItem().equals("--Choose a Request--"))// filter invalid choice
					{
						(new Dialog("Select a request First")).setVisible(true);
					}
					else
					{
				int newdu=Integer.parseInt(setdurationField.getText());
			
				ArrayList<Object> durationSet =new ArrayList<Object>();
				durationSet.add("durationSet");
				durationSet.add(rcomboBox.getSelectedItem().toString());
				durationSet.add(newdu);
				AcademicFrame.myclient.sendMessageToServer(durationSet);
					}
				
			}
		});
		btnSetDuration.setBounds(309, 190, 116, 23);
		add(btnSetDuration);
		
		JButton btnApprove = new JButton("Approve");
		btnApprove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rcomboBox.getSelectedItem().equals("--Choose a Request--"))// filter invalid choice
				{
					(new Dialog("Please Select a Request to Approve")).setVisible(true);
				}
				else
				{
				ArrayList<Object> approve =new ArrayList<Object>();
				approve.add("ApproveEXDu");
				approve.add(rcomboBox.getSelectedItem().toString());
				approve.add("4");
				AcademicFrame.myclient.sendMessageToServer(approve);
				}
			}
		});
		btnApprove.setBounds(59, 244, 89, 23);
		add(btnApprove);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setSize(500, 500);
				frame.setContentPane(supervisorPanel);
			}
		});
		btnCancel.setBounds(287, 244, 89, 23);
		add(btnCancel);
	}

	@Override
	public void display(Object message) {
		// TODO Auto-generated method stub
		ArrayList<Object> answer=new ArrayList<Object>();
		answer.clear(); // clear the info array list
		answer = (ArrayList<Object>)message;
		if(answer.get(0).equals("ridlist"))
		{
			if(answer.get(2).equals("0"))
			{
				(new Dialog("No Requests Available")).setVisible(true);
				frame.setSize(500, 500);
				frame.setContentPane(supervisorPanel);
			}
			else
			{
			int numberOfSystems = Integer.parseInt((answer.get(1)).toString()); // get the number of systems in the list
			this.rcomboBox.addItem("--Choose a Request--");
			for (int i=0; i < numberOfSystems ; i++ )
			{
				this.rcomboBox.addItem((String)answer.get(i+2));  // add systems to the list
			}
			}
		}
			else
				if(answer.get(0).equals("duration"))
				{
					durationField.setText( answer.get(1).toString());
					
				}
				else
					if(answer.get(0).equals("GotIT"))
					{
						(new Dialog("Duration Approved")).setVisible(true);
						frame.setContentPane(new ApproveExecutionDurationPanel(frame, supervisorPanel));
						frame.setSize(460, 420);
						
						ArrayList<Object> req= new ArrayList<Object>();
						req.add("getReq");
						req.add("3");
						AcademicFrame.myclient.sendMessageToServer(req); // get requests
					}
					else
						if(answer.get(0).equals("Fail"))
						{
							(new Dialog("Faild Approving")).setVisible(true);
						}
						else
							if(answer.get(0).equals("updateDuration"))
							{
								(new Dialog("Duration Updated")).setVisible(true);
							}
							else
								if(answer.get(0).equals("unableToupdateDuration"))
								{
									(new Dialog("Unable To Update Duration")).setVisible(true);
								}
				
		}
		
	}


